﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data;

public class ManageBase<T> where T : new()
{
    public static T Get(int ID)
    {
        T model = new T();
        DataTable dt = DBHelper.GetDataTable("select * from [" + model.GetType().Name + "] where ID=" + ID + " and IsDel = 0");
        if (dt.Rows.Count == 0)
            return default(T);
        else
        {
            Type type = model.GetType();
            PropertyInfo[] pil = type.GetProperties();
            for (int i = 0; i < pil.Length; i++)
            {
                pil[i].SetValue(model, DBNull.Value == dt.Rows[0][pil[i].Name] ? "" : dt.Rows[0][pil[i].Name], null);
            }
        }
        return model;
    }


    /// <summary>
    /// 根据ID 获取一个实体类
    /// </summary>
    /// <param name="ID">实体ID</param>
    /// <param name="searchDeleted">获取时判断是否已删除,true为查询已删除,false为不查询已删除,默认false</param>
    /// <returns></returns>
    public static T Get(int ID, bool searchDeleted)
    {
        T model = new T();
        DataTable dt = DBHelper.GetDataTable("select * from [" + model.GetType().Name + "] where ID=" + ID + (searchDeleted ? "" : " and IsDel = 0"));
        if (dt.Rows.Count == 0)
            return default(T);
        else
        {
            Type type = model.GetType();
            PropertyInfo[] pil = type.GetProperties();
            for (int i = 0; i < pil.Length; i++)
            {
                pil[i].SetValue(model, DBNull.Value == dt.Rows[0][pil[i].Name] ? "" : dt.Rows[0][pil[i].Name], null);
            }
        }
        return model;
    }
    public static void Del(int ID)
    {
        T model = new T();
        DBHelper.ExecuteCommand("Update [" + model.GetType().Name + "] set IsDel = 1 where ID=" + ID);
    }
    /// <summary>
    /// 根据ID 删除一个实体类
    /// </summary>
    /// <param name="ID">实体ID</param>
    /// <param name="dataUtility">删除方式,true为物理删除,false为逻辑删除,默认false</param>
    public static void Del(int ID, bool dataUtility)
    {
        T model = new T();
        if (dataUtility)
            DBHelper.ExecuteCommand("Delete [" + model.GetType().Name + "] where ID=" + ID);
        else
            DBHelper.ExecuteCommand("Update [" + model.GetType().Name + "] set IsDel = 1 where ID=" + ID);
    }
    /// <summary>
    /// 添加一个实体类
    /// </summary>
    /// <param name="o">需要加入数据库的实体</param>
    public static void Add(object o)
    {
        Type type = o.GetType();
        string name = "";
        string value = "";
        PropertyInfo[] pil = type.GetProperties();
        for (int i = 1; i < pil.Length; i++)
        {
            if (pil[i].GetValue(o, null) != null)
            {
                name += pil[i].Name + ",";
                value += "N'" + pil[i].GetValue(o, null).ToString().Replace("'", "''") + "',";
            }
        }
        type.GetProperty("ID").SetValue(o, int.Parse(DBHelper.GetDataTable("insert into [" + type.Name + "](" + name.Remove(name.Length - 1) + ") values(" + value.Remove(value.Length - 1) + ")  select IDENT_CURRENT('" + type.Name + "')").Rows[0][0].ToString()), null);
    }
    /// <summary>
    /// 更新一个实体类
    /// </summary>
    /// <param name="o">需要更新的实体</param>
    public static void Set(object o)
    {
        Type type = o.GetType();
        string SQL = "";
        PropertyInfo[] pil = type.GetProperties();
        for (int i = 1; i < pil.Length; i++)
        {
            if (pil[i].GetValue(o, null) != null)
                SQL += pil[i].Name + "=N'" + pil[i].GetValue(o, null).ToString().Replace("'", "''") + "',";
        }
        DBHelper.ExecuteCommand("Update [" + type.Name + "] set " + SQL.Remove(SQL.Length - 1) + " where ID=" + pil[0].GetValue(o, null));
    }
}

public class ManageBase
{
    /// <summary>
    /// 添加一个实体类
    /// </summary>
    /// <param name="o">需要加入数据库的实体</param>
    public static void Add(object o)
    {
        Type type = o.GetType();
        string name = "";
        string value = "";
        PropertyInfo[] pil = type.GetProperties();
        for (int i = 1; i < pil.Length; i++)
        {
            if (pil[i].GetValue(o, null) != null)
            {
                name += pil[i].Name + ",";
                value += "'" + pil[i].GetValue(o, null).ToString().Replace("'", "''") + "',";
            }
        }
        type.GetProperty("ID").SetValue(o, int.Parse(DBHelper.GetDataTable("insert into [" + type.Name + "](" + name.Remove(name.Length - 1) + ") values(" + value.Remove(value.Length - 1) + ")   select IDENT_CURRENT('" + type.Name + "')").Rows[0][0].ToString()), null);
    }

    /// <summary>
    /// 更新一个实体类
    /// </summary>
    /// <param name="o">需要更新的实体</param>
    public static void Set(object o)
    {
        Type type = o.GetType();
        string SQL = "";
        PropertyInfo[] pil = type.GetProperties();
        for (int i = 1; i < pil.Length; i++)
        {
            if (pil[i].GetValue(o, null) != null)
                SQL += pil[i].Name + "='" + pil[i].GetValue(o, null).ToString().Replace("'", "''") + "',";
        }
        DBHelper.ExecuteCommand("Update [" + type.Name + "] set " + SQL.Remove(SQL.Length - 1) + " where ID=" + pil[0].GetValue(o, null));
    }

    /// <summary>
    /// 根据数据表名得到该表的最新排序编号
    /// </summary>
    /// <param name="tablename">数据表名</param>
    /// <returns>最新排序编号</returns>
    public static int GetTableSort(string tablename, string where = "")
    {
        DataTable dt = DBHelper.GetDataTable(string.Format("select count(id) as counts from {0} {1}", tablename, where));
        if (dt != null && dt.Rows.Count > 0)
            return int.Parse(dt.Rows[0]["counts"].ToString()) + 1;
        else
            return 0;
    }
}